var ns,  app,UIAlbumBrowser;
app = new Vue({
    el: '#app',
    data: {
        ID:0,
        form: {
            Type:0,

        },
        imglist: [],
        imgindex: 0,
        select_imglist: [],
        select_imgindex: 0,
        maxuploadcount: CONFIG.maxuploadcount,
        filelist:[],
    },
    methods: {
        //保存
        do_save:function(){
            var that = this;
            if (that.form.Type!=7&&that.form.Type!=8) {
                api.toast({
                    msg: '请选择查验结果通过与否',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            
            // var filelist = [];
            // for (var i = 0; i < that.imglist.length; i++) {
            //     filelist.push(that.imglist[i].url);
            // }
            var options = {};
            options.ID = that.ID;
            options.Type = that.form.Type;
            options.CheckDesc = that.form.CheckDesc;
            options.action = "registeriscomplete";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.form.ProjectID = 0;
                    that.form.CheckDesc = '';
                    that.imglist = [];
                    that.imgindex = 0;
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'chayan_updata',
                    });
                    setTimeout(function(){
                        api.closeWin();
                    }, 500);

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: that.filelist
                }
            });

        },
        //选择通过，不通过
        select_all_active:function(type){
            var that=this;
            that.form.Type=type;
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start: function() {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function(ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        ID: 0,
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath,
                        isupload: true
                    });
                    that.imgindex++;
                }
            });
        },
        UIAlbumBrowser_transPath: function() {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function(ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.do_imageFilter(ret.path);
                    that.imglist.push({
                        ID: 0,
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath,
                        isupload: true
                    });
                    that.imgindex++;
                }
            });
        },
        //图片压缩
        do_imageFilter:function(path){
            var that=this;
            var imgName = Date.now() + '.jpg';
            var imgFullName = 'fs://photo/' + imgName;
            var imageFilter = api.require('imageFilter');
            imageFilter.compress({
                img: path,
                isClarityimg:false,
                quality: 0.2,
                scale:0.5,
                save: {
                    album: true,
                    imgPath: 'fs://photo/',
                    imgName: imgName
                },
            },function( ret, err ){      
                if( ret.status ){
                    // imageFilter.getAttr({
                    //     path:'fs://photo/'+imgName
                    // }, function(ret){
                    //     alert('获取'+JSON.stringify(ret));
                    // })
                    // console.log(JSON.stringify(imgFullName));
                    that.filelist.push(imgFullName);
                }else{
                    alert( JSON.stringify( err ) );
                }
            });
        },
        remove_img: function(index) {
            var that = this;
            for (var i = 0; i < that.imglist.length; i++) {
                var item = that.imglist[i];
                if (item.index == index) {
                    that.imglist.splice(i, 1);
                    if (item.ID > 0) {
                        that.do_remove_uploadimg(item);
                    }
                }
            }
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },


    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.ID = api.pageParam.ID || 0;
    UIAlbumBrowser = api.require('UIAlbumBrowser');
};
